package com.itshidu.jiaowu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itshidu.jiaowu.dao.StudentMapper;
import com.itshidu.jiaowu.dao.UserMapper;
import com.itshidu.jiaowu.entity.Student;
import com.itshidu.jiaowu.entity.User;
import com.itshidu.jiaowu.entity.Violate;
import com.itshidu.jiaowu.dao.ViolateMapper;
import com.itshidu.jiaowu.service.ViolateService;
import com.itshidu.jiaowu.util.LoginUtil;
import com.itshidu.jiaowu.util.Result;
import com.itshidu.jiaowu.vo.ViolateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XiaZengming123
 * @since 2019-07-18
 */
@Service
@Primary
public class ViolateServiceImpl extends ServiceImpl<ViolateMapper, Violate> implements ViolateService {

    @Autowired ViolateMapper violateMapper;
    @Autowired UserMapper userMapper;
    @Autowired StudentMapper studentMapper;


    //新增处分记录  Ps：新增记录时学生姓名必须是学生的用户名
    @Override
    public Result save(String name, Integer deduct_marks, String reason) {

        User loginuser= (User) LoginUtil.getsession().getAttribute("UserInfo");      //获取当前老师信息
        User u =userMapper.SelectByName(name);                                          //查出要扣分的学生用户
        Student student =studentMapper.selectById(u.getId());

        Violate vio = new Violate();                                                    //设置处分记录内容
        vio.setHandlerId(u.getId());
        vio.setStudentId(u.getId());
        vio.setScore(deduct_marks);
        vio.setReason(reason);
        vio.setHandlerId(loginuser.getId());
        vio.setCreated(new Date());
        violateMapper.insert(vio);

        Student stu = studentMapper.selectById(u.getId());
        if (student.getScore().equals(0)){
            return Result.of(202, "该学生的积分已为0");
        }
        if (deduct_marks>student.getScore()){
            return Result.of(201, "分数不足");
        }
        stu.setScore(stu.getScore()-deduct_marks);                                      //扣分
        studentMapper.updateById(stu);

        return Result.of(200, "添加成功");
    }

    //查询所有处分记录
    @Override
    public Result list(int page, int limit) {
        PageHelper.startPage(page,limit);                                   //开始分页
        QueryWrapper<Violate> violatewrapper = new QueryWrapper<>();      //mybatis的Wrapper接口
        List<Violate> list = violateMapper.selectList(violatewrapper);       //查询所有
        PageInfo<Violate> info = new PageInfo<>(list);                      //pageinfo里包含分页信息
        List<ViolateVo> list1=new ArrayList();                                 //新建一个list集合
        for(Violate v:list){
            //获取处理人的名字
            Long Hid=v.getHandlerId();
            User Huser=userMapper.selectById(Hid);
            String Husername=Huser.getUsername();
            //获取学生的名字
            Long Sid=v.getStudentId();
            User Suser=userMapper.selectById(Sid);
            String Susername=Suser.getUsername();

            //设置uservo对象
            ViolateVo uservo=new ViolateVo();
            uservo.setId(v.getId());
            uservo.setHandler(Husername);
            uservo.setStudent(Susername);
            uservo.setScore(v.getScore());
            uservo.setReason(v.getReason());
            uservo.setCreated(v.getCreated());
            list1.add(uservo);                      //存进list里
        }
        return Result.of().put("code", 0)
                .put("msg", "")
                .put("count",info.getTotal())
                .put("data", list1);
    }

    //修改处分记录
    @Override
    public Result EditViolate(Long id, String handler, String name, Integer deduct_marks, String reason) {


        Violate vio = violateMapper.selectById(id);                      //查出要修改的处分记录
        System.out.println("查出来的vio对象==="+vio.toString());
        User handlerUser = userMapper.SelectByName(handler);             //查找老师User
        User user = userMapper.SelectByName(name);                    //查出扣分的学生user
        Student student =studentMapper.selectById(user.getId());

        if (user==null){
            return Result.of(204, "学生姓名不存在");
        }
        if (student.getScore().equals(0)) {
            return Result.of(203, "该学生的积分已为0");
        }
        if (deduct_marks.equals(0)) {
            return Result.of(202, "不能扣0分哦");
        }
        if (deduct_marks > student.getScore()) {
            return Result.of(201, "分数不足");
        }

        vio.setHandlerId(handlerUser.getId());
        vio.setStudentId(user.getId());
        vio.setScore(deduct_marks);
        vio.setReason(reason);
        violateMapper.updateById(vio);
        System.out.println("修改后的的vio==="+vio.toString());

        return Result.of(200, "修改成功");
    }

    @Override
    public Result sorl(Integer page, Integer limit, String studentName) {
        User u=userMapper.SelectByName(studentName);
        if(u==null){
            return Result.of(100,"用户不存在");
        }
        PageHelper.startPage(page, limit);          //开始分页
        if(u.getType().equals("student")) {
            List<Violate> list = violateMapper.SelectBySId(u.getId());
            PageInfo<Violate> info = new PageInfo(list);
            List<ViolateVo> list2 = new ArrayList();
            for (Violate o : list) {
                //获取处理人的名字
                Long Hid = o.getHandlerId();
                User Huser = userMapper.selectById(Hid);
                String Husername = Huser.getUsername();

                //获取学生的名字
                Long Sid = o.getStudentId();
                User Suser = userMapper.selectById(Sid);
                String Susername = Suser.getUsername();

                //设置uservo对象
                ViolateVo uservo = new ViolateVo();
                uservo.setId(o.getId());
                uservo.setHandler(Husername);
                uservo.setStudent(Susername);
                uservo.setScore(o.getScore());
                uservo.setReason(o.getReason());
                uservo.setCreated(o.getCreated());
                list2.add(uservo);                      //存进list里
            }
        return Result.of().put("code", 0)
                .put("msg", "")
                .put("count",info.getTotal())
                .put("data", list2);
        }





        List<Violate> list = violateMapper.SelectBySId2(u.getId());
        PageInfo<Violate> info = new PageInfo(list);
        List<ViolateVo> list2 = new ArrayList();
        for (Violate o : list) {
            //获取处理人的名字
            Long Hid = o.getHandlerId();
            User Huser = userMapper.selectById(Hid);
            String Husername = Huser.getUsername();

            //获取学生的名字
            Long Sid = o.getStudentId();
            User Suser = userMapper.selectById(Sid);
            String Susername = Suser.getUsername();

            //设置uservo对象
            ViolateVo uservo = new ViolateVo();
            uservo.setId(o.getId());
            uservo.setHandler(Husername);
            uservo.setStudent(Susername);
            uservo.setScore(o.getScore());
            uservo.setReason(o.getReason());
            uservo.setCreated(o.getCreated());
            list2.add(uservo);                      //存进list里
        }
        return Result.of().put("code", 0)
                .put("msg", "")
                .put("count",info.getTotal())
                .put("data", list2);
    }


}
